'''

Case Type： 数据库系统
Case Name： 数据库运行时，删除pid文件，停止数据库
Create At:  2020/07/23
@zou_jialiang0505328126
Descption:  1. 启动数据库2. 删除postmaster.pid文件3. 停止数据库(待开发修改)

history：
created： 2020/07/23  created by @wan005
'''
import os
import unittest
from yat.test import Node
import time
import _thread
import queue
from yat.test import macro
import sys

sys.path.append(sys.path[0] + "/../")
from testcase.utils.Logger import Logger
from testcase.utils.Constant import Constant
from testcase.utils.Common import Common
from testcase.utils.CommonSH import *

logger = Logger()
common = Common()
TPCC_RES = queue.Queue()


class HungPidDeletePidfile(unittest.TestCase):
    dbPrimaryUserNode = Node(node='PrimaryDbUser')
    dbPrimaryRootNode = Node(node='PrimaryRoot')
    DB_INSTANCE_PATH = macro.DB_INSTANCE_PATH
    DB_ENV_PATH = macro.DB_ENV_PATH
    GS_CTL_STOP_SUCCESS_MSG = Constant.GS_CTL_STOP_SUCCESS_MSG
    STANDBY_NORMAL = Constant.STANDBY_NORMAL
    PRIMARY_NORMAL = Constant.PRIMARY_NORMAL
    START_SUCCESS_MSG = Constant.START_SUCCESS_MSG
    TPCC_PATH = macro.TPCC_PATH
    DB_POSTMASTER_PID_NAME = macro.DB_POSTMASTER_PID_NAME
    TPCC_SUCCESS_MSG = Constant.TPCC_SUCCESS_MSG
    RESTART_SUCCESS_MSG = Constant.RESTART_SUCCESS_MSG
    sh = CommonSH()
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        logger.info("-----------this is setup-----------")
        logger.info('------------Opengauss_Reliability_Dbsys_Case127.py start------------')

    def test_pid_delete_pidfile(self):
        logger.info("-------------start opengauss")
        startCmd = '''
            source {source_path};
            gs_om -t start'''.format(source_path=self.DB_ENV_PATH)
        logger.info(startCmd)
        startMsg = self.dbPrimaryUserNode.sh(startCmd).result()
        logger.info(startMsg)
        self.assertTrue(startMsg.find(self.START_SUCCESS_MSG) > -1)

        logger.info("-----------delete postmaster.pid -----------")
        deleteCmd = '''
            rm -rf {postmaster}'''.format(postmaster=self.DB_INSTANCE_PATH + '/' + self.DB_POSTMASTER_PID_NAME)
        self.dbPrimaryUserNode.sh(deleteCmd).result()

        logger.info("-----------stop opengauss -----------")
        stopCmd = '''
            source {source_path};
            gs_om -t start'''.format(source_path=self.DB_ENV_PATH)
        logger.info(stopCmd)
        stopMsg = self.dbPrimaryUserNode.sh(stopCmd).result()
        logger.info(stopMsg)
        # self.assertTrue(stopMsg.find(self.START_SUCCESS_MSG) > -1)待开发修改

    def tearDown(self):
        logger.info('----------------this is tearDown-----------------------')
        logger.info("-----------restart opengauss-----------")
        restartCmd = '''
            source {source_path};
            gs_ctl restart -D {DN1_PATH} -m immediate'''.format(source_path=self.DB_ENV_PATH,
                                                                DN1_PATH=self.DB_INSTANCE_PATH)
        logger.info(restartCmd)
        restartMsg = self.dbPrimaryUserNode.sh(restartCmd).result()
        logger.info(restartMsg)
        self.assertTrue(restartMsg.find(self.RESTART_SUCCESS_MSG) > -1)
        time.sleep(10)
